package com.hdy.web.controller;

import com.hdy.web.dao.CommentMapper;
import com.hdy.web.dao.QuestionMapper;
import com.hdy.web.dao.ReplyMapper;
import com.hdy.web.pojo.Question;
import com.hdy.web.pojo.Comment;
import com.hdy.web.pojo.Reply;
import com.hdy.web.utils.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class CommentController {

    final
    ServletContext context;
    final CommentMapper commentMapper;
    final QuestionMapper questionMapper;
    final ReplyMapper replyMapper;
    public CommentController(CommentMapper commentMapper, ServletContext context, QuestionMapper questionMapper, ReplyMapper replyMapper) {
        this.commentMapper = commentMapper;
        this.context = context;
        this.questionMapper = questionMapper;
        this.replyMapper = replyMapper;
    }

    @RequestMapping("/comment")
    public String comment(@RequestParam("qid") int qid, @RequestParam("fromId") String fromId,
                          @RequestParam("content") String content) {
        Comment entity = new Comment();
        commentMapper.insertComment(qid, fromId, content, entity);
        return "forward:detailing";
    }
    @RequestMapping( "/rc")
    public String rc( Model model, HttpSession session){
        String name = (String)session.getAttribute("userName");
        try {
            List<Question> questions = questionMapper.getByName(name);
            List<Comment> lists=new ArrayList<>();
            for (Question question:questions) {
                List<Comment> comments = commentMapper.getSpecific(question.getId());
                lists.addAll(comments);
            }
            //整理集合里的question
            //List<Question> Qs=new ArrayList<>();
            Question[] Qs=new Question[100];
            for (Question ques:questions
                 ) {
                Qs[ques.getId()]=ques;
            }
            //comment按时间排序
            Comment[] ls =lists.toArray(new Comment[lists.size()]);
            Sort.sortComment(ls);
            //把comment和question数据存进model
            model.addAttribute("comments",ls);
            model.addAttribute("questions",Qs);
            return "reply";
        } catch (Exception e) {
            return "reply";
        }
    }


    @RequestMapping("/detailing")
    public String sel(@RequestParam("qid") int qid, Model model) {
        List<Comment> comments;
        try {
            comments = commentMapper.getSpecific(qid);
        } catch (Exception e) {
                return "forward:details";
        }
        int num = comments.size();
        Map<Integer,List<Reply>> map=new HashMap<>();
        for (Comment comment :
                comments) {
            int mid=comment.getMid();
            List<Reply> list= null;
            try {
                list = replyMapper.getById(mid);
                map.put(mid,list);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(list.size()==0){
                map.remove(mid);
            }
        }
        try {
            map.put(0,replyMapper.getById(0));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        model.addAttribute("map",map);
        model.addAttribute("comments", comments);
        model.addAttribute("num", num);
            return "forward:details";
        }
    }

